package com.haois.mp.database.module.remote.service.impl;

import com.google.gson.Gson;
import com.haois.mp.database.util.HttpClientUtil;
import com.haois.mp.database.commom.CommonConstants;
import com.haois.mp.database.module.remote.service.IYellowService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by RuiHaoZ on 2017/12/10.
 */
@Service("iYellowService")
public class YellowServiceImpl implements IYellowService {
    private static final Logger logger = LoggerFactory.getLogger(GroupServiceImpl.class);

    @Value("${defined.database.server-url}")
    private String serverUrl;

    Gson gson = new Gson();

    @Override
    public Map getAllOrg(String accessToken, Map params) {
        String url = serverUrl + CommonConstants.GROUP_FOLLOW_ALL;
        String response = HttpClientUtil.sendGet(url, params, accessToken);
        Map result = gson.fromJson(response, Map.class);
        logger.debug("获取全部公共组织返回结果:{}", response);
        return result;

    }

    @Override
    public Map getFollow(String accessToken) {
        return getFollow(accessToken, new HashMap());
    }

    @Override
    public Map getFollow(String accessToken, Map params) {
        String url = serverUrl + CommonConstants.GROUP_FOLLOW_LIST;
        String response = HttpClientUtil.sendGet(url, params, accessToken);
        Map result = gson.fromJson(response, Map.class);
        logger.debug("获取关注的公共组织返回结果:{}", response);
        return result;
    }

    @Override
    public Map unFollowGroup(String oid, String accessToken) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("oid", oid);
        String url = serverUrl + CommonConstants.GROUP_UN_FOLLOW.replace(CommonConstants.OID, oid);
        String response = HttpClientUtil.sendPost(url, params, accessToken);
        Map result = gson.fromJson(response, Map.class);
        logger.debug("取消关注组织返回结果:{}", response);
        return result;
    }

}
